﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Topro.Util
{
    /// <summary>
    /// 文件工具类
    /// </summary>
    public class FileUtil
    {
        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>存在:true  不存在:false</returns>
        /// <remarks>2013-12-30 苟治国 创建</remarks>
        public static Boolean HasFile(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath)) return false;
            return File.Exists(filePath);
        }

        #region//getFileSize:取得指定文件的大小.
        /// <summary>
        /// 取得指定文件的大小.
        /// </summary>
        /// <param name="FilePath">文件的绝对路径.</param>
        /// <returns>以KB为单位返回文件大小.</returns>
        public static long getFileSize(string FilePath)
        {
            long result = 0;
            if (File.Exists(FilePath))
            {
                FileStream MyFs = new FileStream(FilePath, FileMode.Open);
                result = MyFs.Length / 1024;
                MyFs.Close();
                MyFs.Dispose();
            }
            return result;
        }
        #endregion

        #region//deleteFileFromFolder:删除指定的文件夹中的所有文件.
        /// <summary>
        /// 删除指定的文件夹中的所有文件.
        /// </summary>
        /// <param name="folderPath">文件夹路径.</param>
        /// <returns>返回被删除文件的数量.</returns>
        public static int deleteFileFromFolder(string folderPath)
        {
            int qdkRe = 0;
            try
            {
                string[] fileList = Directory.GetFiles(folderPath);
                for (int ii = 0; ii < fileList.Length; ii++)
                {
                    File.Delete(fileList[ii]);
                    qdkRe++;
                }
            }
            catch (Exception Ex)
            {
                //ErrorLog.WriteLog(Ex);
            }
            return qdkRe;
        }
        #endregion

        #region//deleteFile:删除指定的文件.
        /// <summary>
        /// 删除指定的文件.
        /// </summary>
        /// <param name="filePath">文件路径.</param>
        /// <returns>true/false</returns>
        public static bool deleteFile(string filePath)
        {
            bool result = false;
            if (File.Exists(filePath))
            {
                try
                {
                    File.Delete(filePath);
                    result = true;
                }
                catch (Exception Ex)
                {
                    //ErrorLog.WriteLog(Ex);
                }
            }
            return result;
        }
        #endregion

        #region//deleteDirtory:删除指定的目录.
        /// <summary>
        /// 删除指定的目录.
        /// </summary>
        /// <param name="dirPath">文件路径.</param>
        /// <returns>true/false</returns>
        public static bool deleteDirtory(string dirPath)
        {
            bool result = false;
            if (Directory.Exists(dirPath))
            {
                try
                {
                    Directory.Delete(dirPath, true);
                    result = true;
                }
                catch (Exception Ex)
                {
                    //ErrorLog.WriteLog(Ex);
                }
            }
            return result;
        }
        #endregion

        #region//loadFile:读取指定文件文本内容
        /// <summary>
        /// 读取指定文件文本内容.
        /// </summary>
        /// <param name="filePath">文件路径.</param>
        /// <returns>文本内容</returns>
        public static string loadFile(string filePath)
        {
            return loadFile(filePath, Encoding.Default);
        }
        public static string loadFile(string filePath, Encoding textEncode)
        {
            string result = "";
            if (File.Exists(filePath))
            {
                try
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    StreamReader sr = new StreamReader(fs, textEncode);
                    result = sr.ReadToEnd();
                    sr.Close();
                    fs.Close();

                }
                catch (Exception ex)
                {
                    //ErrorLog.WriteLog(Ex);
                }

            }
            return result;
        }
        #endregion

        #region 写入文件
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public static bool writerFile(string filePath, string content)
        {
            var result = false;

            try
            {
                //文件流
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
                //写入流
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(content);
                sw.Close();
                fs.Close();

                result = true;
            }
            catch (Exception ex)
            {
                //ErrorLog.WriteLog(Ex);
            }

            return result;
        }
        #endregion
    }
}
